<?php
/**
 * IndexController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Session/Namespace.php';;

class WinnerController extends Zend_Controller_Action
{
    /**
     * The default action - show the home page
     */
    public function indexAction()
    {
    	$winnerService = Application_Service_Winner::getInstance();
    	
    	$winnerNum = $winnerService->getWinnerNum(); // 获得中拍总人数
    	$winnerNum = $winnerNum['winner_num'];
    	
    	$this->view->winnerNum = $winnerNum;
    	
    	if(!isset($_GET['page'])) {         // 登录历史竞拍，没有带参数
    		
    		$page = ceil($winnerNum/Application_Model_Constants::$WINNER_PAGE_NUM);
    		
    	} else {                            // 选择页数，带参数
    		
    		$page = $_GET['page'];
    		
    		// 检测page参数是否为数字，是否大于中拍页数，或小于0
    		if(!is_numeric($page) || ($page - 1) * Application_Model_Constants::$WINNER_PAGE_NUM > $winnerNum || $page <= 0){
    			$this->_redirect('/winner'); 
    			return;
    		}
    	}
    	
    	// 获得上一页，下一页的页数
    	$max_page = ceil($winnerNum/Application_Model_Constants::$WINNER_PAGE_NUM);
    	$previous_page = $page - 1;
    	$next_page = $page + 1;
    	
    	if($next_page > $max_page) {
    		$next_page = 0;
    	}
    	
    	$this->view->page = $page;
    	$this->view->previous_page = $previous_page;
    	$this->view->next_page = $next_page;
    	
    	/////////////////////
		// 读取最新一页的中拍信息
		$this->view->winners = $winnerService->getWinner(($page - 1)* Application_Model_Constants::$WINNER_PAGE_NUM);
    }
}
